<!--
 * @Description: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2022-01-19 10:18:43
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2022-01-19 10:40:55
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>两个数组的交集</title>
</head>

<body>
    <script>
        var intersection = function (nums1, nums2) {
            // 交换数组长度，去最长的来做外层遍历
            if (nums1.length < nums2.length) {
                const _ = nums1;
                nums1 = nums2;
                nums2 = _;
            }
            // set去重
            const nums1Set = new Set(nums1);
            const reSet = new Set();
            // 是否存在包含关系的数组
            for (let i = nums2.length - 1; i > -0; i--) {
                nums1Set.has(nums2[i]) && reSet.add(nums[i]);
            }
            return Array.from(reSet);
        }
    </script>

</body>

</html>